package com.dongdongshop.search.service.impl;


import com.alibaba.dubbo.config.annotation.Reference;
import com.dongdongshop.seller.pojo.TbItem;
import com.dongdongshop.seller.service.ItemService;
import io.searchbox.client.JestClient;
import io.searchbox.core.Index;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.io.IOException;
import java.util.List;

@Component
@Transactional
@RocketMQMessageListener(topic = "goods_goodsId",consumerGroup = "dongdongconsumer")
public class SearchMQ implements RocketMQListener<Long> {

    private Logger logger = LoggerFactory.getLogger(SearchMQ.class);

    @Reference
    private ItemService itemService;
    @Autowired
    private JestClient jestClient;

    @Override
    public void onMessage(Long goodsId) {
        System.out.println("搜索，MQ消息接收成功："+goodsId);

        List<TbItem> items = itemService.selectbygoodsid(goodsId);
        for (TbItem item : items) {
            Index build = new Index.Builder(item).index("dongdongshop").type("item").id(item.getId().toString()).build();
            try {
                jestClient.execute(build);
            }catch (IOException e){
                logger.error("上传es出现异常",e);
                e.printStackTrace();
            }
        }
    }
}
